////////////////////////////////////////////////////////////////////////////////
//
//	Original file Name: SpriteSheet.java
//	Originating Author: JWhitefield
//	
//	Description: Represents a sheet of image assets 
//
////////////////////////////////////////////////////////////////////////////////

package com.jjw.game.graphics;

import java.awt.image.BufferedImage;

/**
 * Represents a sheet of image assets 
 * 
 * @author JWhitefield
 *
 */
public class SpriteSheet {
	
	//--------------------------------------------------------------------------
	//
	//Properties
	//
	//--------------------------------------------------------------------------
		
	//----------------------------------
	// Public
	//----------------------------------
	
	//----------------------------------
	//  Getters / Setters
	//----------------------------------
	
	//----------------------------------
	// Private
	//----------------------------------
	
	/**
	 * 3D array of sprite's which represent our sprite sheet 
	 */
	private Sprite[][] sprites = null;
	
	/**
	 * Amount of rows on the sprite sheet
	 */
	private int rowCount = 0;
	
	/**
	 * Amount of columns on the sprite sheet 
	 */
	private int columnCount = 0;
	
	//--------------------------------------------------------------------------
	//
	//  Methods
	//
	//--------------------------------------------------------------------------
	
	//----------------------------------
	// Public
	//----------------------------------
	
	/**
	 * Creates and reads the sprite sheet
	 * 
	 * @param spriteSheet	the buffered image which represents the sprite sheet
	 * @param spriteWidth   the width of the sprite images
	 * @param spriteHeight	the height of the sprite images
	 */
	SpriteSheet( BufferedImage spriteSheet, int spriteWidth, int spriteHeight ) {
		
		//Read the sprite sheet
		this.readSpriteSheet(spriteSheet, spriteWidth, spriteHeight);
	}
	
	/**
	 * Gets the image at these coordinates 
	 *  
	 * @param row		the row position
	 * @param column	the column position 
	 * @return			the sprite, if it wasn't found null
	 */
	Sprite getImageAt( int row, int column ) {
		
		//Check that the array has been created 
		if( this.sprites == null )
			return null;
		
		//The sprite we will return 
		Sprite foundSprite = null;
		
		//Enclose within try and catch as its much easier to do this
		//then try and work out if its in range etc so if it fails null will
		//be returned
		try{
			
			//Get the sprite form these chords and use that 
			foundSprite = this.sprites[row][column];
			
		}catch (Exception e) {}
		
		//Return the found sprite 
		return foundSprite;
	}
	
	//----------------------------------
	// Private
	//----------------------------------

	/**
	 * Gets a collection of sprite's from the sprite sheet 
	 * 
	 * @param spriteSheet	the sprite sheet itself
	 * @param spriteWidth	width of the sprite on the sheet
	 * @param spriteHeight	height of the sprite on the sheet 
	 * 
	 * @return	array of sprite's which where generated from the sheet 
	 */
	private void readSpriteSheet(BufferedImage spriteSheet, int spriteWidth, int spriteHeight  ) {
		
		//Get the amount of columns and rows
		this.rowCount = spriteSheet.getHeight() / spriteHeight;
		this.columnCount = spriteSheet.getWidth() / spriteWidth;
		
		//Create the list of sprite's we are going to collect 
		this.sprites = new Sprite[ this.rowCount ][ this.columnCount ]; 
		
		//Blank sprite 
		Sprite sprite = null;
		
		//Loop through the rows of sprite's 
		for( int i = 0; i < this.rowCount; i++ ){
			
			//Loop through the columns 
			for( int j = 0; j < this.columnCount; j++ ) {
			
				//Get the sprite image from the sprite sheet 
				BufferedImage spriteImage = spriteSheet.getSubimage(j * spriteWidth , i * spriteHeight , spriteWidth, spriteHeight);

				//Create a new sprite with all the info
				sprite = new Sprite( spriteImage, j, i);
				
				//Add the sprite into the array of harvested sprite's 
				this.sprites[i][j] = sprite;
			}
		}
	}
	
	//----------------------------------
	// Overridden / Implemented
	//----------------------------------
}
